from rest_framework import serializers
from .models import Todo

class TodoSerializer(serializers.ModelSerializer):
    """待办事项序列化器"""
    
    # 传递display 给前端显示
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    priority_display = serializers.CharField(source='get_priority_display', read_only=True)
    
    class Meta:
        model = Todo
        fields = ['id', 'title', 'description', 'status', 'status_display', 
                 'priority', 'priority_display', 'due_date', 'created_at', 
                 'updated_at', 'user']
        read_only_fields = ['user', 'created_at', 'updated_at']

    def create(self, validated_data):
        """创建时自动设置用户"""
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)
